package com.devunion.salon.client.app.mvc.controller;

import com.devunion.salon.client.app.mvc.SalonEvents;
import com.devunion.salon.client.app.mvc.view.AppView;
import com.extjs.gxt.ui.client.mvc.AppEvent;
import com.extjs.gxt.ui.client.mvc.Controller;
import com.extjs.gxt.ui.client.mvc.View;

/**
 * @author Timoshenko Alexander 27.02.2009 : 23:29:37
 */
public class AppController extends Controller {
    private View appView;

    public AppController() {
        registerEventTypes(SalonEvents.COMPONENT_INIT);
        registerEventTypes(SalonEvents.APP_INIT);
    }

    public AppController(View appView) {
        this();
        this.appView = appView;
    }

    public void setAppView(View appView) {
        this.appView = appView;
    }

    protected void initialize() {
        if (appView == null) {
            appView = new AppView(this);
        }
        addChild(new CompanyController());
        addChild(new ProductListController());
        addChild(new ProductController());
        addChild(new VendorController());
    }

    public void handleEvent(AppEvent<?> event) {
        switch (event.type) {
            case SalonEvents.APP_INIT:
                forwardToView(appView, event);
                break;
            case SalonEvents.COMPONENT_INIT:
                forwardToChild(event);
                break;
            default:
                forwardToChild(event);
        }
    }
}
